package com.example.hlddz2.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.hlddz2.R;
import com.example.hlddz2.utils.PageRouter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProfileFragment extends Fragment {

    // 功能菜单数据
    private int[] featureIcons = {
            R.drawable.ic_collect,
            R.drawable.ic_coupon,
            R.drawable.ic_address,
            R.drawable.ic_setting
    };

    private String[] featureTitles = {
            "我的收藏",
            "优惠券",
            "收货地址",
            "设置"
    };

    // 订单状态数据
    private int[] orderIcons = {
            R.drawable.ic_order_all,
            R.drawable.ic_order_pay,
            R.drawable.ic_order_delivery,
            R.drawable.ic_order_receive,
            R.drawable.ic_order_comment
    };

    private String[] orderTitles = {
            "全部",
            "待付款",
            "待发货",
            "待收货",
            "待评价"
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_profile, container, false);

        // 初始化用户信息区域
        initUserInfo(root);

        // 初始化功能菜单
        initFeatureMenu(root);

        // 初始化订单状态
        initOrderStatus(root);

        return root;
    }

    private void initUserInfo(View root) {
        ImageView ivAvatar = root.findViewById(R.id.iv_avatar);
        TextView tvUsername = root.findViewById(R.id.tv_username);
        TextView tvLogin = root.findViewById(R.id.tv_login);

        // 登录按钮点击事件
        tvLogin.setOnClickListener(v -> {
            Toast.makeText(getContext(), "跳转到登录页面", Toast.LENGTH_SHORT).show();
            // 实际项目中应跳转到登录Activity
        });

        // 头像点击事件
        ivAvatar.setOnClickListener(v -> {
            if (tvUsername.getText().equals("游客用户")) {
                Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getContext(), "修改头像", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initFeatureMenu(View root) {
        GridView gvFeatures = root.findViewById(R.id.gv_features);

        // 准备数据
        List<Map<String, Object>> data = new ArrayList<>();
        for (int i = 0; i < featureIcons.length; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("icon", featureIcons[i]);
            item.put("title", featureTitles[i]);
            data.add(item);
        }

        // 创建适配器
        SimpleAdapter adapter = new SimpleAdapter(
                getContext(),
                data,
                R.layout.item_profile_menu,
                new String[]{"icon", "title"},
                new int[]{R.id.iv_icon, R.id.tv_title}
        );

        gvFeatures.setAdapter(adapter);

        // 设置点击事件
        gvFeatures.setOnItemClickListener((parent, view, position, id) -> {
            switch (position) {
                case 0:
                    PageRouter.goToCollection(getContext());
                    break;
                case 1:
                    PageRouter.goToCoupon(getContext());
                    break;
                case 2:
                    PageRouter.goToAddress(getContext());
                    break;
                case 3:
                    PageRouter.goToSetting(getContext());
                    break;
            }
        });
    }

    private void initOrderStatus(View root) {
        GridView gvOrders = root.findViewById(R.id.gv_orders);

        // 准备数据
        List<Map<String, Object>> data = new ArrayList<>();
        for (int i = 0; i < orderIcons.length; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("icon", orderIcons[i]);
            item.put("title", orderTitles[i]);
            data.add(item);
        }

        // 创建适配器
        SimpleAdapter adapter = new SimpleAdapter(
                getContext(),
                data,
                R.layout.item_profile_menu,
                new String[]{"icon", "title"},
                new int[]{R.id.iv_icon, R.id.tv_title}
        );

        gvOrders.setAdapter(adapter);

        // 设置点击事件
        gvOrders.setOnItemClickListener((parent, view, position, id) -> {
            switch (position) {
                case 0:
                    PageRouter.goToAllOrder(getContext());
                    break;
                case 1:
                    PageRouter.goToUnpaid(getContext());
                    break;
                case 2:
                    PageRouter.goToUnshipped(getContext());
                    break;
                case 3:
                    PageRouter.goToUnreceived(getContext());
                    break;
                case 4:
                    PageRouter.goToUncommented(getContext());
                    break;
            }
        });
    }
}